package com.we.service.impl;

import com.we.dao.CategoryDao;
import com.we.entity.Category;
import com.we.entity.PageBean;
import com.we.entity.Tag;
import com.we.service.CategoryService;
import com.we.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Description 标签业务逻辑接口实现类
 * @Author cr7
 * @Date 10:40
 * @Version V1.0
 */
public class CategoryServiceImpl implements CategoryService {

    private SqlSession session = MybatisUtils.getSqlSession();
    private CategoryDao dao = session.getMapper(CategoryDao.class);

    /**
     * 分页查询分类名称及其数量功能实现
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Category> findAllCategoryByPage(int currentPage, int pageSize) {
        PageBean<Category> pb = new PageBean<>();
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        int totalCount = dao.findCategoryCount();
        pb.setTotalCount(totalCount);

        //设置当前页显示的数据集合
        int start = (currentPage - 1) * pageSize;//开始的记录数
        List list = dao.findAllCategoryByPage(start, pageSize);
        pb.setList(list);

        //设置总页数 = 总记录数/每页显示条数
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    /**
     * 添加分类功能实现
     * @param categoryName
     * @return
     */
    @Override
    public int addCategory(String categoryName) {
        return dao.addCategory(categoryName);
    }

    /**
     * 查询分类功能实现
     * @param categoryName
     * @return
     */
    @Override
    public int findCategoryByCategoryName(String categoryName) {
        return dao.findCategoryByCategoryName(categoryName);
    }

    /**
     * 删除分类功能实现
     * @param categoryName
     * @return
     */
    @Override
    public int deleteCategoryByCategoryName(String categoryName) {
        return dao.deleteCategoryByCategoryName(categoryName);
    }

    /**
     * 添加分类信息功能实现
     * @param categoryName
     * @param noteID
     * @return
     */
    @Override
    public int saveCategory(String categoryName, int noteID) {
        return dao.saveCategory(categoryName,noteID);
    }

    /**
     * 修改分类名称功能实现
     * @param categoryName
     * @param categoryName1
     * @return
     */
    @Override
    public int updateCategoryName(String categoryName, String categoryName1) {
        return dao.updateCategoryName(categoryName,categoryName1);
    }

    /**
     * 查询分类名称及其数量功能实现
     * @return
     */
    @Override
    public List findAllCategory() {
        return dao.findAllCategory();
    }

    /**
     * 查询分类数量功能实现
     * @return
     */
    @Override
    public int findCategoryCount() {
        return dao.findCategoryCount();
    }
}
